package com.zhiqingchun.web.sidebar;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhiqingchun.dao.comment.ICommentDao;
import com.zhiqingchun.pojo.CommentDO;

@Controller
@RequestMapping("/sidebar.html")
public class SidebarController {

	@RequestMapping(method = RequestMethod.GET)
	public String get(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		CommentDO commentDO = new CommentDO();
		List<CommentDO> commentList = commentDao.list(commentDO, null);
		if (commentList.size() > 5) {
			commentList = commentList.subList(0, 5);
		}
		map.addAttribute("commentList", commentList);
		
		return "sidebar/sidebar";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String post(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		return get(map, request, response);
	}
	
	@Autowired
	private ICommentDao commentDao;

	public ICommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(ICommentDao commentDao) {
		this.commentDao = commentDao;
	}
}
